package org.example.ttms.entityClass;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.example.ttms.customize.BuyTicket;
import org.example.ttms.entityClass.ticket.Ticket;
import org.example.ttms.entityClass.train.Station;
import org.example.ttms.entityClass.train.Trips;

import java.io.*;
import java.util.*;

public class User {
    private String id;
    private String name;
    private String account;
    private String password;
    private List<String> ticketList = new ArrayList<>();

    public User(String id, String account, String password) {
        this.id = id;
        this.account = account;
        this.password = password;
    }

    public User() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<String> getTicketList() {
        return ticketList;
    }

    public void setTicketList(List<String> ticketList) {
        this.ticketList = ticketList;
    }

    //根据用户account获取整个用户
    public User obtainUser(String account){
        try {
            String file = "src/main/java/org/example/ttms/file/user.txt";
            Scanner scan = new Scanner(new FileReader(file));
            while(scan.hasNextLine()){
                String str = scan.nextLine();
                ObjectMapper objectMapper = new ObjectMapper();
                User user = objectMapper.readValue(str, User.class);
                if(user.account.equals(account)){
                    return user;
                }
            }
        } catch (FileNotFoundException | JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
    //获取所有用户---key为id
    public HashMap<String, User> obtainAllUser(){
        HashMap<String, User> hashMap = new HashMap<>();
        try {
            String file = "src/main/java/org/example/ttms/file/user.txt";
            Scanner scan = new Scanner(new FileReader(file));
            while(scan.hasNextLine()){
                String str = scan.nextLine();
                ObjectMapper objectMapper = new ObjectMapper();
                User user = objectMapper.readValue(str, User.class);
                hashMap.put(user.id, user);
            }
        } catch (FileNotFoundException | JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return hashMap;
    }
    //获取所有用户---key为name
    public HashMap<String, User> obtainAllUser2(){
        HashMap<String, User> hashMap = new HashMap<>();
        try {
            String file = "src/main/java/org/example/ttms/file/user.txt";
            Scanner scan = new Scanner(new FileReader(file));
            while(scan.hasNextLine()){
                String str = scan.nextLine();
                ObjectMapper objectMapper = new ObjectMapper();
                User user = objectMapper.readValue(str, User.class);
                hashMap.put(user.name, user);
            }
        } catch (FileNotFoundException | JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return hashMap;
    }
    //注册时给一个用户id
    public String obtainId(){
        HashMap<String, User> hashMap = obtainAllUser();
        String enrollId = "User0";
        for(Map.Entry<String, User> map : hashMap.entrySet()){
            enrollId = map.getKey();
        }
        return "User" + (Integer.parseInt(enrollId.split("User")[1]) + 1);
    }
    //添加用户
    public void addUser(User user){
        ObjectMapper objectMapper = new ObjectMapper();
        String file = "src/main/java/org/example/ttms/file/user.txt";
        try {
            String jsonString = objectMapper.writeValueAsString(user);
            System.out.println("addUser:"+jsonString);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter
                    (new FileOutputStream(file, true)));
            writer.write(jsonString+"\n");
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //买票添加
    public void addBuyTicket(Ticket ticket){
        ticketList.add(ticket.getId());
        HashMap<String, User> hashMap = obtainAllUser();
        hashMap.put(id, this);
        storageUser(FXCollections.observableArrayList(hashMap.values()));
    }
    //重新储存用户信息---覆盖
    public void storageUser(ObservableList<User> observableList){
        ObjectMapper objectMapper = new ObjectMapper();
        String file = "src/main/java/org/example/ttms/file/user.txt";
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter
                    (new FileOutputStream(file)));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        for(User user : observableList) {
            try {
                String jsonString = objectMapper.writeValueAsString(user);
                writer.write(jsonString + "\n");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try {
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //删除所有用户关于指定车次的票
    public void delTripsAllTicket(Trips trips){
        Ticket ticket = new Ticket();
        List<Ticket> list = ticket.obtainTripsAllTicket(trips);
        HashMap<String, User> hashMap = obtainAllUser2();
        for(Ticket ticket1 : list){
            String name = ticket1.getBuyerName();
            if(name != null){
                User user = hashMap.get(name);
                user.ticketList.remove(ticket1.getId());
                hashMap.put(name, user);
            }
        }
        storageUser(FXCollections.observableArrayList(hashMap.values()));
    }
}
